#include "stdafx.h"
#include "riftObject.h"


initialiseSingleton(cGuidMgr);

DWORD IGameFun::GetGuidSingleton()
{
    uFunCall uu = {0};
    uu.pfn = WOW_GET(eGuidSingleton);
    return uu._fnParm0Ret();
}

DWORD IGameFun::GetPlayerGuid()
{
    uFunCall uu = {0};
    uu.pfn = WOW_GET(ePlayerGuid);
    return uu._fnParm0Ret();
}

void cGuidMgr::Init()
{
    //union 
    //{
    //    DWORD value;
    //    cGuidMgr* type;
    //} uu;
    //uu.value = *(DWORD*)WOW_GET(eThis);
    //uu.value ^= WOW_GET(eThisXOR);
    //cGuidMgr* dwBase = uu.type;
    //mSingleton = dwBase;
    mSingleton = (cGuidMgr*)IGameFun::GetGuidSingleton();
}

void cGuidMgr::Release()
{
    mSingleton = NULL;
}

HANDLE cGuidMgr::GetPoint(const sGuid &guid, DWORD index)
{ 
    __try
    {
        uThisCall ut = {0};
        ut.pfn = (HANDLE)WOW_GET(eGetPoint);
        return (this->*ut._getPoint)(guid, index);
    }
    __except(1)
    {

    }
    return NULL;
}

void cGuidMgr::GetGuids(VecGuids &vec, DWORD index)
{
    cHashTable** pAry = NULL;  
    //union
    //{
    //    DWORD value;
    //    cHashTable** type;
    //}uu;
    //uu.value = WOW_GET(eArycHashTable3);
    //uu.value += (DWORD)this;
    //pAry = uu.type;
    
    pAry = GetAddr<cHashTable*>(WOW_GET(eArycHashTable3));
    pAry[index]->GetGuid(vec);
}

void cGuidMgr::GetGuids(ListGuids &list, DWORD index)
{
    cHashTable** pAry = NULL;  
    //union
    //{
    //    DWORD value;
    //    cHashTable** type;
    //}uu;
    //uu.value = WOW_GET(eArycHashTable3);
    //uu.value += (DWORD)this;
    //pAry = uu.type;
    pAry = GetAddr<cHashTable*>(WOW_GET(eArycHashTable3));
    pAry[index]->GetGuid(list);
}

sGuid cGuidMgr::PlayerGuid()
{
    union 
    {
        DWORD value;
        sGuid* type;
    } uu;
    //uu.value = *(DWORD*)WOW_GET(eThis_8);
    //uu.value ^= WOW_GET(eThis_8XOR);
    uu.value = IGameFun::GetPlayerGuid();
    uu.value += WOW_GET(ePlayGuid);
    return *uu.type;
}

sPos cPosInfo::GetPos()
{
    sPos pos;
    pos.x = GetValue<float>(WOW_GET(ePosX));
    pos.z = GetValue<float>(WOW_GET(ePosZ));
    pos.y = GetValue<float>(WOW_GET(ePosY));
    float fSin = GetValue<float>(WOW_GET(eSin));
    float fCos = GetValue<float>(WOW_GET(eCos));

    float fPi = atan2(fSin, fCos);
    if (fPi<0)
    {
        fPi += 2*PI;
    }
    pos.f = fPi;

    return pos;
}

void cPosInfo::ModifyHeading(float angle)
{
    float fSin = sin(angle);
    float fCos = cos(angle);
    SetValue<float>(fSin, WOW_GET(eSin));
    SetValue<float>(fCos, WOW_GET(eCos));
    //union
    //{
    //    DWORD value;
    //    float* f;
    //}uu;
    //uu.value = WOW_GET(eSin) +(DWORD)this;
    //*uu.f = fSin;
    //uu.value = WOW_GET(eCos) +(DWORD)this;
    //*uu.f = fCos;
}

DWORD cObject::Data58()
{
    //union
    //{
    //    DWORD value;
    //    cObject* _this;
    //}uu;
    //uu._this = this;
    //uu.value += WOW_GET(eUnk58);
    //uu.value = *(DWORD*)uu.value;
    DWORD tt = GetValue<DWORD>(WOW_GET(eUnk58));
    return tt;
}

sGuid cObject::GetGuid()
{
    return GetValue<sGuid>(WOW_GET(eGuid));
    //union
    //{
    //    DWORD value;
    //    sGuid* type;
    //    cObject* _this;
    //} uu;
    //uu._this = this;
    //uu.value += WOW_GET(eGuid);
    //return *uu.type;
}

 sPos cObject::GetPos()
 {
    union
    {
        DWORD data;
        cPosInfo* pPosInfo;
    }uu;
    uu.data = Data58()+WOW_GET(ecPosInfo);
    uu.data = *(DWORD*)uu.data;

    sPos pos = uu.pPosInfo->GetPos();
    return pos;
 }

 void cUnit::ModifyHeading(float angle)
 {
     union
     {
         DWORD data;
         cPosInfo* pPosInfo;
     }uu;
     uu.data = Data58()+WOW_GET(ecPosInfo);
     uu.data = *(DWORD*)uu.data;
     
     uu.pPosInfo->ModifyHeading(angle);
 }